﻿using Cinch;
using MEFedMVVM.ViewModelLocator;
using System;
using System.ComponentModel.Composition;
using FR.SAFTAnalyser.Services;

namespace FR.SAFTAnalyser.ViewModels
{
    [ExportViewModel("SettingsViewModel")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class SettingsViewModel : WorkspaceViewModel
    {
        IOpenFileService openFileService;

        [ImportingConstructor]
        public SettingsViewModel(IOpenFileService ofs)
        {
            this.openFileService = ofs;
            this.DisplayName = "Opções";

            //Commands
            DoSaveSettingsCommand = new SimpleCommand<Object, Object>(CanSave, SaveSettings);
            DoOpenSaftFileCommand = new SimpleCommand<Object, Object>(CanOpenSaftFile, OpenSaftFile);
            DoOpenPublicKeyFileCommand = new SimpleCommand<Object, Object>(CanOpenPublicKeyFile, OpenPublicKeyFile);
        }

        bool isDirty;

        public string SAFTFileName
        {
            get { return Properties.Settings.Default.SAFTFileName; }
            set
            {
                Properties.Settings.Default.SAFTFileName = value;
                isDirty = true;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<SettingsViewModel>(x => x.SAFTFileName));
            }
        }

        public int SchemaVersion
        {
            get { return Properties.Settings.Default.SchemaVersion; }
            set
            {
                if (value >= 0)
                {
                    Properties.Settings.Default.SchemaVersion = value;
                    isDirty = true;
                    NotifyPropertyChanged(ObservableHelper.CreateArgs<SettingsViewModel>(x => x.SchemaVersion));
                }
            }
        }

        public string PublicKeyFileName
        {
            get { return Properties.Settings.Default.PublicKeyFileName; }
            set
            {
                Properties.Settings.Default.PublicKeyFileName = value;
                isDirty = true;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<SettingsViewModel>(x => x.PublicKeyFileName));
            }
        }

        public bool RememberLastOpenFiles
        {
            get { return Properties.Settings.Default.RememberLastOpenFiles; }
            set
            {
                Properties.Settings.Default.RememberLastOpenFiles = value;
                isDirty = true;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<SettingsViewModel>(x => x.RememberLastOpenFiles));
            }
        }

        public DateTime DataMinima
        {
            get { return Properties.Settings.Default.MinDate; }
            set
            {
                Properties.Settings.Default.MinDate = value;
                isDirty = true;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<SettingsViewModel>(x => x.DataMinima));
            }
        }

        #region Commands
        public SimpleCommand<object, object> DoSaveSettingsCommand { get; private set; }

        public void SaveSettings(Object args)
        {
            Properties.Settings.Default.Save();
            isDirty = false;
        }

        public bool CanSave(Object args)
        {
            return isDirty;
        }

        public SimpleCommand<object, object> DoOpenSaftFileCommand { get; private set; }

        public void OpenSaftFile(Object args)
        {
            if (openFileService != null)
                this.SAFTFileName = openFileService.OpenFile("SAFT Files (*.xml)|*.xml");
        }

        public bool CanOpenSaftFile(Object args)
        {
            return RememberLastOpenFiles;
        }

        public SimpleCommand<object, object> DoOpenPublicKeyFileCommand { get; private set; }

        public void OpenPublicKeyFile(Object args)
        {
            if (openFileService != null)
                this.PublicKeyFileName = openFileService.OpenFile("Public Key files (.txt) | *.txt");
        }

        public bool CanOpenPublicKeyFile(Object args)
        {
            return RememberLastOpenFiles;
        }
        #endregion Commands
    }
}
